package com.elinkway.infinitemovies.async;

/**
 * Created by zhaopeipei on 2016/3/12.
 */

import android.content.Context;

import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.bean.VideoDataBean;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.VideoDetailParser;
import com.elinkway.infinitemovies.http.parser.VideoListParser;
import com.elinkway.infinitemovies.ui.activity.VideoDetailActivity;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.lvideo.http.bean.LVideoDataHull;

/**
 * 请求剧集信息
 *
 * @author daipei
 */
public class RequestVideoTask extends MoviesHttpAsyncTask<VideoDataBean> {

    private String aid;

    Context videoContext;

    String videoSite;

    String videoPorder;

    Boolean mIsFinishPlayer;

    public RequestVideoTask(Context context, String aid, String site, String porder, Boolean isFinishPlayer) {
        super(context);
        videoContext = context;
        this.aid = aid;
        videoSite = site;
        mIsFinishPlayer = isFinishPlayer;
        if (porder.isEmpty()) {
            videoPorder = "1";
        } else {
            videoPorder = porder;
        }
    }

    @Override
    public void netNull() {
        super.netNull();
    }

    @Override
    public void netErr(int updateId, String errMsg) {
        super.netErr(updateId, errMsg);
    }

    @Override
    public void dataNull(int updateId, String errMsg) {
        super.dataNull(updateId, errMsg);
    }

    public RequestVideoTask(Context context) {
        super(context);
    }

    @Override
    public LVideoDataHull<VideoDataBean> doInBackground() {
        LVideoDataHull<VideoDataBean> ret = MoviesHttpApi.requestVideoData(
                new VideoDetailParser(), aid, "");
        LogUtils.e("", "Enter PlayActivitySelfOwn Video Task");
        if (ret.getDataType() == LVideoDataHull.DataType.DATA_IS_INTEGRITY) {
            VideoDataBean v = ret.getDataEntity();
            LogUtils.e("", "Enter PlayActivitySelfOwn Video Task INT");
        }
        return ret;
    }

    @Override
    public void onPostExecute(int updateId, VideoDataBean result) {
        VideoDataBean video;
        video = result;
        aid = video.getAid();
        RequestVideoListTask requestVideoList;
        requestVideoList = new RequestVideoListTask(videoContext, videoSite, videoPorder, video);
        requestVideoList.start();
    }


    private class RequestVideoListTask extends
            MoviesHttpAsyncTask<VideoDataBean> {
        String videoListTaskVideoSite;
        String videoListTaskVideoPorder;
        VideoDataBean video;
        Context videoContext;
        public RequestVideoListTask(Context context, String site, String porder, VideoDataBean vd) {
            super(context);
            videoContext = context;
            video = vd;
            videoListTaskVideoSite = site;
            videoListTaskVideoPorder = porder;
        }

        @Override
        public void netNull() {
            super.netNull();
        }

        @Override
        public void netErr(int updateId, String errMsg) {
            super.netErr(updateId, errMsg);
        }

        @Override
        public void dataNull(int updateId, String errMsg) {
            super.dataNull(updateId, errMsg);
        }

        @Override
        public LVideoDataHull<VideoDataBean> doInBackground() {
            return MoviesHttpApi.requestVideoList(new VideoListParser(videoListTaskVideoSite, video, 1),
                    videoListTaskVideoSite, video.getAid(), 1, 100);
        }

        @Override
        public void onPostExecute(int updateId, VideoDataBean result) {
            // 更新剧集信息
            video.setEpisodeList(result.getEpisodeList());
            video.setSitename(result.getPlaySrcBean().getSitename());
            LogUtils.e("", "Enter PlayActivitySelfOwn END " + video.getName() + " " + result.getSitename());
            if ((MoviesApplication.VIDEO_DETAIL_THREAD.equals(MoviesApplication.getInstance().getRunningActivityName()) &&
                    MoviesApplication.getInstance().getVideoDetailActivity() != null)
                    || (mIsFinishPlayer && MoviesApplication.getInstance().getVideoDetailActivity() != null)) {
                MoviesApplication.getInstance().getVideoDetailActivity().finish();
            }
            VideoDetailActivity.launch(MoviesApplication.getInstance().getActivity(), video.getAid(),
                    video.getVt(), video.getName(), "", video.getThemeId(), "", "0","","");
        }
    }

}